//
//  ViewController.swift
//  诚叙缘
//
//  Created by sujeking on 17/3/20.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit

class CXYMainViewController: UIViewController {
    
    var pageController : UIPageViewController!
    var homeViewController :CXYBaseNavigationController!
    var msgViewController : CXYBaseNavigationController!
    var shopViewController : CXYBaseNavigationController!
    var myInfoViewController : CXYBaseNavigationController!
    var selectView : UIView!
    
    
    
    @IBOutlet weak var homebtn: CXYTabBarButton!
    @IBOutlet weak var msgbtn: CXYTabBarButton!
    @IBOutlet weak var shopbtn: CXYTabBarButton!
    @IBOutlet weak var myinfobtn: CXYTabBarButton!
    @IBOutlet weak var btnsBGView: UIView!
    
    @IBOutlet weak var hbImageView: UIImageView!
    
    
    var vcs : NSMutableArray!
    var btns : NSMutableArray!
    
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let appdelegate = UIApplication.shared.delegate as! AppDelegate
        appdelegate.mainViewController = self
    }
    
    func setupUI() {
        initViewControllers()
        pageController.dataSource = self;
        pageController.delegate = self;
        
        pageController.setViewControllers([homeViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
        btns = NSMutableArray(array: [homebtn,msgbtn,shopbtn,myinfobtn])
        
        selectView = UIView(frame: self.homebtn.bounds)
        selectView.bounds.size.width = UIScreen.main.bounds.width / 4
        selectView.backgroundColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
        btnsBGView.insertSubview(selectView, belowSubview: homebtn)
    }
    
    func initViewControllers() {
        vcs = NSMutableArray()
        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
        
        pageController = self.childViewControllers.first as! UIPageViewController
        homeViewController = storyboard.instantiateViewController(withIdentifier: "homenv") as! CXYBaseNavigationController
        msgViewController = storyboard.instantiateViewController(withIdentifier: "msgnv") as! CXYBaseNavigationController
        shopViewController = storyboard.instantiateViewController(withIdentifier: "shopnv") as! CXYBaseNavigationController
        myInfoViewController = storyboard.instantiateViewController(withIdentifier: "myinfonv") as! CXYBaseNavigationController
        vcs.addObjects(from: [homeViewController,msgViewController,shopViewController,myInfoViewController])
    }
 
    // MARK: - Action
 
    @IBAction func loadHomeVC(_ sender: Any) {
        loadVC(viewIndex: (sender as AnyObject).tag - 100)
    }
    
    @IBAction func loadMsgVC(_ sender: Any) {
        loadVC(viewIndex: (sender as AnyObject).tag - 100)
    }
    
    @IBAction func loadShopVC(_ sender: Any) {
        loadVC(viewIndex: (sender as AnyObject).tag - 100)
    }
    
    
    @IBAction func loadMyInfoVC(_ sender: Any) {
        loadVC(viewIndex: (sender as AnyObject).tag - 100)
    }
    
    func loadVC(viewIndex:Int) {
        
        var changeType : UIPageViewControllerNavigationDirection!
        
        let lastIndex = vcs.index(of: pageController.viewControllers?.first as Any)
        
        if viewIndex > lastIndex {
            changeType = UIPageViewControllerNavigationDirection.forward
        } else {
            changeType = UIPageViewControllerNavigationDirection.reverse
        }
        
        pageController.setViewControllers([vcs.object(at: viewIndex) as! UIViewController], direction: changeType, animated: true) { (flag) in
            self.updateBtnStatus(index: viewIndex)
        }
    }
    
    open func hiddenTabBar(next:@escaping ()->Void) {
        let pointX = UIScreen.main.bounds.midX
        UIView.animate(withDuration: 0.3, animations: {
            self.btnsBGView.center = CGPoint(x: pointX, y: 1000)
        }) { (flag) in
            if flag {
                next()
            }
        }
    }
    
    open func showTabBar() {
        let showPointy = UIScreen.main.bounds.maxY - btnsBGView.bounds.height / 2
        let pointX = UIScreen.main.bounds.midX
        
        UIView.animate(withDuration: 0.5) {
            self.btnsBGView.center = CGPoint(x: pointX, y: showPointy)
        }
    }
}

extension CXYMainViewController : UIPageViewControllerDataSource {
    //返回当前页面的下一个页面
    //返回当前页面的上一个页面
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        var index = vcs.index(of: viewController)
        if index == 0 || index == NSNotFound {
            return nil
        }
        index -= 1
        return vcs.object(at: index) as? CXYBaseNavigationController
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var index = vcs.index(of: viewController)
        
        if index == NSNotFound || index == vcs.count - 1 {
            return nil
        }
        index += 1
        return vcs.object(at: index) as? CXYBaseNavigationController
    }
}

extension CXYMainViewController :UIPageViewControllerDelegate {
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        let vc = pageController.viewControllers?.first
        let index = vcs.index(of: vc as Any)

        updateBtnStatus(index: index)
    }
    
    func updateBtnStatus(index:Int) {
        if index < 0 || index > btns.count {
            return
        }
        for btn in btns {
            (btn as! CXYTabBarButton).isSelected = false;
        }
        let btn = btns.object(at: index) as! CXYTabBarButton;
        btn.isSelected = true
        
        let point = (btns[index] as! UIButton).center
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: UIViewAnimationOptions(rawValue: 0), animations: {
            self.selectView.center = point
        }, completion: nil)
    }
}

extension CXYMainViewController {
    open func setModuleType(moduleType:Int) {
        if moduleType == 2 {
            self.hbImageView.image = #imageLiteral(resourceName: "shop_bottom")
        }
    }
}

